﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="JobBase.cs">
//   Copyright 2004-2014 DHGMS Solutions.
//      
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//      
//   http://www.apache.org/licenses/LICENSE-2.0
//      
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// <summary>
//   
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Jobs
{
    using System;
    using Dhgms.TplHelper.Model.Info.JobResult;

    /// <summary>
    /// JobBase class for the job handler
    /// </summary>
    /// <typeparam name="TJobResultClass">
    /// The Job Result Type
    /// </typeparam>
    /// <typeparam name="TJobSettings">
    /// The Job Settings Type
    /// </typeparam>
    public abstract class JobBase<TJobResultClass, TJobSettings>
        where TJobResultClass : Base, new() where TJobSettings : class
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="JobJobBase{TJobResultClass,TJobSettings}"/> class. 
        /// </summary>
        /// <param name="guid">
        /// Unique ID for the Job
        /// </param>
        /// <param name="friendlyName">
        /// Friendly Name for the Job
        /// </param>
        protected JobBase(Guid guid, string friendlyName)
        {
            if (guid == null || guid.Equals(Guid.Empty))
            {
                throw new ArgumentNullException("guid");
            }

            if (string.IsNullOrWhiteSpace(friendlyName))
            {
                throw new ArgumentNullException("friendlyName");
            }

            this.Guid = guid;
            this.FriendlyName = friendlyName;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the friendly Name for the Job
        /// </summary>
        public string FriendlyName { get; private set; }

        /// <summary>
        /// Gets the Unique ID for the Job
        /// </summary>
        public Guid Guid { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Entry point for Job Execution
        /// </summary>
        /// <param name="settings">
        /// Settings for the job.
        /// </param>
        public TJobResultClass Execute(TJobSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            return this.OnExecute(settings);
        }

        /// <summary>
        /// The on execute event, where the donkey work should be done
        /// </summary>
        /// <param name="applicationSettings">The application settings</param>
        /// <returns>Job Result Data</returns>
        protected abstract TJobResultClass OnExecute(TJobSettings applicationSettings);

        #endregion
    }
}